﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YJKArchUtils.BreakWallByLevel;
using YJKRevitBase.WinForm;
using YJKRevitView;

namespace YJKRevitWallModule.WallModule
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	internal class CmdBreakWallByLevel : IExternalCommand
	{
		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		public Result Execute(ExternalCommandData cmdData, ref string message, ElementSet elements)
		{
			CmdViewType types = (CmdViewType)127;
			if (SwitchView.SetUsableView(types, cmdData) != Autodesk.Revit.UI.Result.Succeeded)
			{
				return Autodesk.Revit.UI.Result.Failed;
			}
			this.m_cmdData = cmdData;
			this.m_doc = cmdData.Application.ActiveUIDocument.Document;
			this.m_selection = cmdData.Application.ActiveUIDocument.Selection;
			RevitVersionFuncs.ClearSelection(this.m_selection);
			if (CmdBreakWallByLevel._hWndRevit == null)
			{
				CmdBreakWallByLevel._hWndRevit = new WindowHandle(Process.GetCurrentProcess().MainWindowHandle);
			}
			this.InitLevelList();
			BreakWallByLevelForm breakWallByLevelForm = new BreakWallByLevelForm(cmdData, this.m_levels);
			breakWallByLevelForm.Show(CmdBreakWallByLevel._hWndRevit);
			int wMsg = 7;
			CmdBreakWallByLevel.SendMessage(CmdBreakWallByLevel._hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
			Result result;
			try
			{
				for (;;)
				{
					WallFilter wallFilter = new WallFilter();
					IList<Element> list = null;
					try
					{
						list = this.m_selection.PickElementsByRectangle(wallFilter, "请框选需要打断的墙");
					}
					catch
					{
						breakWallByLevelForm.Close();
						result = Autodesk.Revit.UI.Result.Succeeded;
						break;
					}
					if (list.Count != 0)
					{
						bool flag = false;
						List<Wall> list2 = new List<Wall>();
						foreach (Element element in list)
						{
							Wall wall = element as Wall;
							if ((int)wall.WallType.Kind == 2)
							{
								flag = true;
								break;
							}
							list2.Add(wall);
						}
						if (flag)
						{
							TaskDialog.Show("盈建科", "此命令不支持叠层墙。");
						}
						else
						{
							List<Level> list3 = new List<Level>();
							foreach (int index in breakWallByLevelForm.GetSelectLevelIndex())
							{
								list3.Add(this.m_levels.GetByIndex(index) as Level);
							}
							if (list2.Count == 0)
							{
								TaskDialog.Show("盈建科", "当前选择的墙无需分层。");
							}
							else
							{
								List<ElementId> list4 = new List<ElementId>();
								new WallBreakManager(this.m_doc).BreakByLevel(list2, list3, ref list4, true, CmdBreakWallByLevel._hWndRevit);
							}
						}
					}
				}
			}
			catch (Exception)
			{
				breakWallByLevelForm.Close();
				result = Autodesk.Revit.UI.Result.Failed;
			}
			return result;
		}

		private void InitLevelList()
		{
			FilteredElementIterator elementIterator = new FilteredElementCollector(this.m_doc).OfClass(typeof(Level)).GetElementIterator();
			elementIterator.Reset();
			while (elementIterator.MoveNext())
			{
				Element element = elementIterator.Current;
				Level level = element as Level;
				if (level != null)
				{
					this.m_levels.Add(level.Elevation, level);
				}
			}
		}

		private ExternalCommandData m_cmdData;

		private Document m_doc;

		private Selection m_selection;

		public static WindowHandle _hWndRevit;

		private SortedList m_levels = new SortedList();
	}
}
